﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SSS.RFIDWrapper;
using SSS.Classes;
using SSS.DB;


namespace SSS.Scan
{



    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        RFIDWriteWrapper RFIDHandler;
        Customer CurrentCustomer;
        Int32 LastCustomerID;
        DBAccess db;
        

        public MainWindow()
        {
            InitializeComponent();
            RFIDHandler = new RFIDWriteWrapper();
        }


        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            RFIDHandler.CloseReader();
        }

        private void btnKundeSpeichern_Click(object sender, RoutedEventArgs e)
        {
            //if ((dfNachname.Text != "") && (dfVorname.Text != ""))
            //{
                CurrentCustomer = new Customer();
                CurrentCustomer.Prename = dfVorname.Text;
                CurrentCustomer.Name = dfNachname.Text;

                if (kinectBodyMeasurement1.MeasuredLengthLeftFullArm > 0)
                    CurrentCustomer.Armlaenge = Convert.ToInt32(kinectBodyMeasurement1.MeasuredLengthLeftFullArm * 10);

                if (kinectBodyMeasurement1.MeasuredLengthLeftLeg > 0)
                    CurrentCustomer.Beinlaenge = Convert.ToInt32(kinectBodyMeasurement1.MeasuredLengthLeftLeg * 10);


                if (KundeSpeichern(CurrentCustomer))
                {
                    lblKundeSpeichernResult.Content = LastCustomerID.ToString();// "Kunde erstellt";                    
                }
                else
                {
                    lblKundeSpeichernResult.Content = "Fehler!";
                }
            //}
            //else
                //lblKundeSpeichernResult.Content = " Alle Felder ausfüllen!";
            
        }

        private bool KundeSpeichern(Customer kunde)
        {
            if (db == null)
                db = new DBAccess();
            try
            {
                LastCustomerID = db.saveCustomer(kunde);
                return true;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                return false;
            }
            
            
        }

        private void btnRFIDSchreiben_Click(object sender, RoutedEventArgs e)
        {
            // Test um die Kunden-ID an den Reader zu senden
            // TODO: lastCustomerID verwenden. Die Umwandlung in das richtige Format von int in string
            // ist Aufgabe des RFID-Wrappers

            //string tmpStr = "0001";
            if (RFIDHandler.SetCustomerID(LastCustomerID))
            {
                lblRFIDResult.Content = "Erfolgreich!";
            }
            else
            {
                lblRFIDResult.Content = "Fehler!";
            }
        }

        private void btnReset_Click(object sender, RoutedEventArgs e)
        {
            Reset();
        }

        private void Reset()
        {
            dfVorname.Clear();
            dfNachname.Clear();
            lblRFIDResult.Content = "";
            lblRFIDResult.Content = "";
            
            btnMessungStarten.IsEnabled = true;
        }

        private void btnMessungStarten_Click(object sender, RoutedEventArgs e)
        {
            lblRFIDResult.Content = "";
            lblRFIDResult.Content = "";
            kinectBodyMeasurement1.StartMeasurement();
            btnMessungStarten.IsEnabled = false;
        }

        private void kinectBodyMeasurement1_KinectMeasurementCompleted()
        {
            // fertig mit Messung
            // prüfen ob Messwerte da sind
            if ((kinectBodyMeasurement1.MeasuredLengthLeftFullArm > 0) || (kinectBodyMeasurement1.MeasuredLengthLeftLeg > 0))
            {


                kinectBodyMeasurement1.ShowBigGreenMessage("Messung erfolgreich!");

                //Messung erfolgreich -> Kunde In Datenbank
                CurrentCustomer = new Customer();
                CurrentCustomer.Prename = dfVorname.Text;
                CurrentCustomer.Name = dfNachname.Text;

                if (kinectBodyMeasurement1.MeasuredLengthLeftFullArm > 0)
                    CurrentCustomer.Armlaenge = Convert.ToInt32(kinectBodyMeasurement1.MeasuredLengthLeftFullArm * 10);

                if (kinectBodyMeasurement1.MeasuredLengthLeftLeg > 0)
                    CurrentCustomer.Beinlaenge = Convert.ToInt32(kinectBodyMeasurement1.MeasuredLengthLeftLeg * 10);


                if (KundeSpeichern(CurrentCustomer))
                {
                    kinectBodyMeasurement1.ShowBigGreenMessage("Messdaten gespeichert!");
                    //Wenn Kunde in DatenBank gespeichert -> RFID-Chip schreiben
                    lblKundeSpeichernResult.Content = "Kundennummer: "+ LastCustomerID.ToString();// "Kunde erstellt";    
                    if (RFIDHandler.SetCustomerID(LastCustomerID))
                    {
                        lblRFIDResult.Content = "Karte erstellt!";
                        kinectBodyMeasurement1.ShowBigGreenMessage("Karte erstellt!");
                    }
                    else
                    {
                        lblRFIDResult.Content = "Karte nicht beschreibbar!";
                        kinectBodyMeasurement1.ShowBigRedMessage("Karte nicht beschreibbar!");
                    }
                }
                else
                {
                    kinectBodyMeasurement1.ShowBigRedMessage("Datenspeicherung nicht erfolgreich!");
                    lblKundeSpeichernResult.Content = "Kunde nicht speicherbar!";
                }




            }
            else
            {
                //btnKundeSpeichern.IsEnabled = false;
                //btnRFIDSchreiben.IsEnabled = false;
                kinectBodyMeasurement1.ShowBigRedMessage("Messung nicht erfolgreich!");
                MessageBox.Show("Es konnten leider keine Messergebnisse ermittelt werden! Probieren Sie es nocheinmal!");
            }

            btnMessungStarten.IsEnabled = true;
        }

 
    }
}
